package com.toplabs.unashu.controller;


import com.toplabs.unashu.annotation.ControllerEndpoint;
import com.toplabs.unashu.common.response.CommonResponse;
import com.toplabs.unashu.dto.PageDto;
import com.toplabs.unashu.entity.Type;
import com.toplabs.unashu.exception.ServiceException;
import com.toplabs.unashu.service.TypeService;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ityanger
 * @since 2020-08-23
 */
@CrossOrigin
@RestController
public class TypeController {
    private final TypeService typeService;

    public TypeController(TypeService typeService) {
        this.typeService = typeService;
    }

    @GetMapping("/types/limit")
    public CommonResponse<PageDto<Type>> types(@RequestParam Integer offset, @RequestParam Integer limit) {
        PageDto<Type> pages = typeService.listTypeBlogCount(offset, limit);
        if (null == pages) {
            return CommonResponse.failed();
        }
        return CommonResponse.success(pages);
    }

    @GetMapping("/types")
    public CommonResponse<PageDto<Type>> types() {
        PageDto<Type> pages = typeService.listTypeBlogCount(null, null);
        if (null == pages) {
            return CommonResponse.failed();
        }
        return CommonResponse.success(pages);
    }

    @GetMapping("/types/list")
    public CommonResponse<Object> typeList() {
        List<Type> types = typeService.listType();
        if (null == types) {
            return CommonResponse.failed();
        }
        return CommonResponse.success(types);
    }

    @GetMapping("/type/{id}")
    public CommonResponse<Type> selectType(@PathVariable("id") Long id) {
        Type type = typeService.selectType(new Type(id, null, 0, null));
        if (null == type) {
            return CommonResponse.failed();
        }
        return CommonResponse.success(type);
    }

    @PostMapping("/type")
    @Transactional(rollbackFor = Exception.class)
    @ControllerEndpoint(operation = "添加分类", exceptionMessage = "添加分类失败")
    public CommonResponse<String> saveType(@RequestBody Type type) {
        Type types = typeService.selectType(type);
        if (null == types) {
            int res = typeService.insertType(type);
            if (res == 0) {
                throw new ServiceException("添加失败，请重试！");
            }
            return CommonResponse.success("添加成功！");
        } else {
            throw new ServiceException("添加失败，不能重复添加分类！");
        }
    }

    @PutMapping("/type")
    @Transactional(rollbackFor = Exception.class)
    @ControllerEndpoint(operation = "更新分类", exceptionMessage = "更新分类失败，请检查是否重复")
    public CommonResponse<Type> updateType(@RequestBody Type type) throws NotFoundException {
        Type types = typeService.selectType(type);
        if (null != types) {
            return CommonResponse.failed("更新失败，分类不能重复");
        }
        typeService.updateType(type);
        return CommonResponse.success(type);
    }

    @DeleteMapping("/type/{id}")
    @Transactional(rollbackFor = Exception.class)
    @ControllerEndpoint(operation = "删除分类", exceptionMessage = "删除分类失败")
    public CommonResponse<String> deleteType(@PathVariable Long id) {
        int res = typeService.deleteType(id);
        if (res == 0) {
            return CommonResponse.failed("删除失败，请重试！");
        }
        return CommonResponse.success("删除成功！");
    }
}
